<?php

namespace Database\Seeders;

use App\Models\Admin\MenuModel;
use App\Models\Admin\RoleModel;
use App\Models\Admin\UserModel;
use Illuminate\Database\Seeder;

class AdminTablesSeeder extends Seeder
{
    protected array $menus = [
        [
            'component' => 'Layout',
            'title' => '系统设置',
            'code' => 'SysSetting',
            'path' => '/system-setting',
            'icon' => 'Setting',
            'always_show' => 1,
            'children' => [
                [
                    'component' => 'CLayout',
                    'title' => '用户管理',
                    'path' => '/user',
                    'icon' => 'User',
                    'children' => [
                        [
                            'component' => 'admin-setting/user/list.vue',
                            'title' => '用户列表',
                            'code' => 'UserIndex',
                            'type' => 2,
                            'path' => '/index',
                            'icon' => 'User',
                        ],
                        [
                            'component' => 'admin-setting/user/create.vue',
                            'title' => '用户编辑',
                            'code' => 'UserCreate',
                            'type' => 2,
                            'path' => '/create',
                            'hidden' => 1,
                        ],
                    ]
                ],
                [
                    'component' => 'admin-setting/role/list.vue',
                    'title' => '角色管理',
                    'code' => 'Role',
                    'type' => 2,
                    'path' => '/role',
                    'icon' => 'Cherry',
                ],
                [
                    'component' => 'admin-setting/menu/list.vue',
                    'title' => '菜单管理',
                    'code' => 'Menu',
                    'type' => 2,
                    'path' => '/menu',
                    'icon' => 'Menu',
                ],
                [
                    'component' => 'admin-setting/permission/list.vue',
                    'title' => '权限管理',
                    'code' => 'Permission',
                    'type' => 2,
                    'path' => '/permission',
                    'icon' => 'Lock',
                ]
            ],
        ]
    ];

    protected array $role = [
        'name' => '超级管理员',
        'slug' => 'super-admin',
    ];

    protected array $user = [
        'account' => 'admin',
        'nickname' => '超级管理员',
        'password' => '123456',
    ];

    protected int $id = 1;

    /**
     * Run the database seeds.
     *
     * @return void
     * @throws \Throwable
     */
    public function run()
    {
        UserModel::query()->first()?->roles()->detach();
        UserModel::query()->truncate();
        RoleModel::query()->truncate();
        MenuModel::query()->truncate();

        foreach ($this->getMenus($this->menus) as $menu) {
            MenuModel::query()->insert($menu);
        }
        RoleModel::query()->insert($this->role);
        $this->user['password'] = password_hash($this->user['password'], PASSWORD_DEFAULT);
        UserModel::query()->insert($this->user);
        UserModel::query()->first()->roles()->save(RoleModel::query()->first());
    }

    protected function getMenus(array $data, int $pid = 0, int $level = 0): array
    {
        $temp = [];
        foreach ($data as $datum) {
            $id = $this->id++;
            $temp[] = ['id' => $id, 'pid' => $pid] + array_filter($datum, function ($key) {
                    return $key !== 'children';
                }, ARRAY_FILTER_USE_KEY);
            if (isset($datum['children'])) {
                $temp = array_merge($temp, $this->getMenus($datum['children'], $id, $level + 1));
            }
        }
        return $temp;
    }
}
